package com.sx.service.serviceImpl;

import com.sx.dao.SupplierDao;
import com.sx.dao.daoImpl.SupplierDaoImpl;
import com.sx.pojo.Category;
import com.sx.pojo.OptionRecords;
import com.sx.pojo.Supplier;
import com.sx.service.OptionRecordService;
import com.sx.service.SupplierService;

import java.util.ArrayList;

public class SupplierServiceImpl implements SupplierService {
    private final SupplierDao supplierDao = new SupplierDaoImpl();
    private final OptionRecordService optionRecordService = new OptionRecordServiceImpl();

    /**
     * 获取所有供应商信息
     * 此方法通过调用supplierDao的getAllSupplier方法来获取所有的供应商信息
     * 它没有参数，返回一个Supplier对象的ArrayList列表
     * 如果没有供应商信息，则返回一个空的ArrayList
     */
    @Override
    public ArrayList<Supplier> getAllSupplier() {
        return supplierDao.getAllSupplier();
    }

    /**
     * 获取所有类别
     * 此方法用于从数据库中获取所有可用的类别信息它委托SupplierDao中的相应方法来执行实际的数据检索操作
     *
     * @return 返回一个包含所有类别的ArrayList如果数据库中没有类别，则返回空列表
     */
    @Override
    public ArrayList<Category> getAllCategory() {
        return supplierDao.getAllCategory();
    }

    @Override
    public boolean checkIdExists(String id) {
        return supplierDao.checkIdExists(id);
    }

    /*@Override
    public boolean addSupplier(Supplier supplier) {
        return supplierDao.addSupplier(supplier);
    }*/
    public boolean addSupplier(Supplier supplier, String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords OptionRecords = new OptionRecords();
        OptionRecords.setBeOption(null);
        OptionRecords.setAfOption(supplier.toString());
        OptionRecords.setPersonId(PersonId);
        OptionRecords.setPersonName(PersonName);
        OptionRecords.setFlag(flag);
        OptionRecords.setType(type);
        if (supplierDao.addSupplier(supplier)) {
            optionRecordService.addOptionRecord(OptionRecords.getPersonId(), OptionRecords.getPersonName(), OptionRecords.getBeOption(), OptionRecords.getAfOption(), OptionRecords.getFlag(), OptionRecords.getType());
            return true;
        } else {
            return false;
        }
    }

    /*@Override
    public boolean deleteSupplierById(String id) {
        return supplierDao.deleteSupplierById(id);
    }*/
    public boolean deleteSupplierById(String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords OptionRecords = new OptionRecords();
        OptionRecords.setBeOption(supplierDao.getSupplierById(id).toString());
        OptionRecords.setAfOption(null);
        OptionRecords.setPersonId(PersonId);
        OptionRecords.setPersonName(PersonName);
        OptionRecords.setFlag(flag);
        OptionRecords.setType(type);
        if (supplierDao.deleteSupplierById(id)) {
            optionRecordService.addOptionRecord(OptionRecords.getPersonId(), OptionRecords.getPersonName(), OptionRecords.getBeOption(), OptionRecords.getAfOption(), OptionRecords.getFlag(), OptionRecords.getType());
            return true;
        } else {
            return false;
        }

    }


    @Override
    public Supplier getSupplierById(String id) {
        return supplierDao.getSupplierById(id);
    }

    /*@Override
    public boolean updateSupplier(Supplier supplier) {
        return supplierDao.updateSupplier(supplier);
    }*/
    public boolean updateSupplier(Supplier supplier, String PersonId, String PersonName, int flag, int type, String id) {
        OptionRecords OptionRecords = new OptionRecords();
        OptionRecords.setBeOption(supplierDao.getSupplierById(id).toString());
        OptionRecords.setAfOption(supplier.toString());
        OptionRecords.setPersonId(PersonId);
        OptionRecords.setPersonName(PersonName);
        OptionRecords.setFlag(flag);
        OptionRecords.setType(type);
        if (supplierDao.updateSupplier(supplier)) {
            optionRecordService.addOptionRecord(OptionRecords.getPersonId(), OptionRecords.getPersonName(), OptionRecords.getBeOption(), OptionRecords.getAfOption(), OptionRecords.getFlag(), OptionRecords.getType());
            return true;
        } else {
            return false;
        }
    }
}
